package ro.dta.idbi.model.dynamicpattern;

/**
 * Partition class, used to represent a partition of the vertices into classes
 * 
 * @author Tudor Dobrila
 * 
 */
class Partition {
	private int[][] classes;
	private int[][] countVectors;

	/**
	 * Partition class constructor
	 * 
	 * @param noVertices
	 *            No. of vertices that this partition holds
	 */
	public Partition(int noVertices) {
		classes = new int[2][];
		classes[0] = new int[noVertices];
		classes[1] = new int[noVertices];
		countVectors = new int[2][];
		countVectors[0] = new int[noVertices];
		countVectors[1] = new int[noVertices];
	}

	/**
	 * Partition class constructor
	 * 
	 * @param classes
	 *            Initial assignment of vertices to classes
	 */
	public Partition(int[][] classes) {
		this.classes = classes;
		int noVertices = classes[0].length;
		countVectors = new int[2][];
		countVectors[0] = new int[noVertices];
		countVectors[1] = new int[noVertices];
		for (int i = 0; i < noVertices; i++) {
			countVectors[0][classes[0][i] - 1]++;
			countVectors[1][classes[1][i] - 1]++;
		}
	}

	/**
	 * Check if the two count vectors contains the same elements If not,
	 * graphs are not isomorphic
	 * 
	 * @return True if the two count vectors have the same elements, false
	 *         otherwise
	 */
	public boolean isClassesEqual() {
		for (int i = 0; i < countVectors[0].length; i++) {
			if (countVectors[0][i] != countVectors[1][i]) {
				return false;
			}
		}

		return true;
	}

	/**
	 * Set the no. of the class at a given position (i, j)
	 */
	public void setClassAt(int i, int j, int classNo) {
		classes[i][j] = classNo;
	}

	/**
	 * Get the class of the element at (i, j)
	 */
	public int getClassAt(int i, int j) {
		return classes[i][j];
	}

	/**
	 * Get the partition information of the graph at index <i>graph</i>
	 * 
	 * @param graph
	 *            Index of the graph for which to retrieve the partition
	 *            information
	 * @return Partition information for the graph passed as parameter
	 */
	public int[] getClass(int graph) {
		return classes[graph];
	}

	/**
	 * Set the no. of elements in the class at (i, j)
	 */
	public void setCountAt(int i, int j, int count) {
		countVectors[i][j] = count;
	}

	/**
	 * Get the no. of elements in the class at (i, j)
	 */
	public int getCountAt(int i, int j) {
		return countVectors[i][j];
	}
}
